/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaclass;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Vincent
 */
public class LeastSquares {
    private double a,b;

    public double getA() {
        return a;
    }

    public double getB() {
        return b;
    }

    public void solve (double x [], double y [], int n) {
    double D = (n*sumtwo(x,x,n)-Math.pow(sumone(x,n),2));
    b = (sumtwo(x,x,n)*sumone(y,n) -
         sumtwo(x,y,n)*sumone(x,n))/D;
    a = (n*sumtwo(x,y,n)-sumone(x,n)*sumone(y,n))/D;
    }

    private double sumone (double u [], int n) {
    double sofar = 0;
    for (int i=0; i<n; i++)
      sofar += u[i];
    return sofar;
    }

    private double sumtwo (double u [], double v [], int n) {
    double sofar = 0;
    for (int i=0; i<n; i++)
      sofar += u[i]*v[i];
    return sofar;
    }

}
